/*
  Implementation of string and memory related function used in Horizon
*/

#include <types.h>
#include <string.h>


void *memmove(void *dest, const void *src, size_t n)
  {
    /* mem areas may overlap */  
    byte_t *srcp,*dstp;
    size_t i;
    
    srcp=(byte_t *)src;
    dstp=(byte_t *)dest;
    
    for (i=0;i<n;i++)
      *(dstp+i)=*(srcp+i);
    
    return dest;
  }

void *memcpy(void *dest, const void *src, size_t n)
  {
    /* mem areas may _NOT_ overlap ??? */
    return memmove(dest,src,n);
  }

void *memsetb(void *dest, byte_t val)
  {
    /* set  8 bit value */
    byte_t *dstp; 

    dstp=(byte_t *)dest;
    *dstp=val;

    return dest;    
  }

void *memsetw(void *dest, word_t val)
  {
    /* set 16 bit value */
    word_t *dstp; 

    dstp=(word_t *)dest;
    *dstp=val;

    return dest;    
  }

void *memsetd(void *dest, dword_t val)
  {
    /* set 32 bit value */
    dword_t *dstp; 

    dstp=(dword_t *)dest;
    *dstp=val;

    return dest;    
  }

void *memset(void *s, int c, size_t n)
  {
    int i;

    for (i=0;i<n;i++)
      memsetd(s+i,(dword_t)c);

    return s;
  }

byte_t memgetb(void *dest)
  {
    /* get  8 bit value */
    byte_t val;

    val=*((byte_t *)dest);
    return val;
  }

word_t memgetw(void *dest)
  {
    /* get 16 bit value */
    word_t val;

    val=*((word_t *)dest);
    return val;
  }

dword_t memgetd(void *dest)
  {
    /* get 32 bit value */
    dword_t val;

    val=*((dword_t *)dest);
    return val;
  }

size_t strlen(const char *s)
  {
    /* calculates length of a string */
    return (size_t)(index(s,0)-s);
  }

char *index(const char *s, int c)
  {
    /* locate character in string */
    char *p;
    
    for (p=(char *)s;*p!=(char)c;p++);

    return p;
  }

char *strcpy(char *dst,const char *src)
  {
    int len;

    len=strlen(src)+1;
    memcpy(dst,src,len);

    return dst;
  }
